{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0341b846",
   "metadata": {},
   "source": [
    "### 练习：购物车程序\n",
    "* 1.运行代码后，让用户输入支付宝余额，然后打印我们的商品列表给用户\n",
    "* 2.让用户输入商品编号进行商品的购买\n",
    "* 3.用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户\n",
    "* 4.用户可以随时退出购买，退出时打印用户已购买的商品和支付余额"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f95093f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 商 品列表数据\n",
    "good_list = [\n",
    "    # 商品的样式：[商品名称，商品价格]\n",
    "    ['珍珠奶茶',7],\n",
    "    ['大杯珍珠奶茶',8.5],\n",
    "    ['加大杯珍珠奶茶',9.5],\n",
    "    ['桶装珍珠奶茶',20]\n",
    "]\n",
    "# 写到某一步\n",
    "# 用户输入：input\n",
    "Alipay = input(\"请输入您的余额\")\n",
    "\n",
    "# 打印商品信息，打印商品的次数是由商品列表的长度决定的(len(goods_list))\n",
    "for i in range(len(goods_list)):\n",
    "    # 给格式化的样式（先给）\n",
    "    # 样式\n",
    "    # \n",
    "     print(\"商品编号：%s，商品的名称：%s，商品的价格：%s 元\"%(i,goods_list[i][0],goods_list[i][1]))\n",
    "    \n",
    "\n",
    "# 让用户输入商品编号进行商品的购买\n",
    "\n",
    "# 用户输入商品编号\n",
    "choice = input(\"请输入正确的商品编号进行购买(如果退出请输入“q”)：\")\n",
    "if choice.isdigit(): # isdigit判断字符串是否是数值\n",
    "    choice = int(choice) # 如果是数值，把它变成真正的int\n",
    "    # 检查商品编号是否在我们的范围内\n",
    "    if choice >= len(goods_list):\n",
    "        print(\"【您输入的商品不存在，请检查再次输入】\")\n",
    "    else:\n",
    "        # 对比商品编号，找到相应商品：商品价格：goods_list[choice][1],\n",
    "        # 1.检查用户余额 VS 商品价格关系\n",
    "        if Alipay > goods_list[choice][1]:\n",
    "           # 买得起 1.扣钱 ，2。把商品加到用户购物列表\n",
    "           Alipay = Alipay - goods_list[choice][1]\n",
    "        user_shopping_cart.append(goods_list[choice])\n",
    "        print(\"【%s】已经加入您的购物车，您的【余额】还有%s元\"%(goods_list[choice][0],Alipay))\n",
    "        else:\n",
    "            print(\"您的余额不足以购买，请更换支付方式\")\n",
    "    elif choice == \"q\"\n",
    "    # 如果用户输入q 则说明用户要退出购买了，我们将打印用户已购买的商品列表以及用户余额\n",
    "    print(\"您的购物车：\",user_shopping_cart,'\\',\"您的用户余额: %s 元\")%(Alipay)\n",
    "    break\n",
    "else:\n",
    "    print(\"请输入正确的【商品编码】或者按照输入提示进行输入\")\n",
    "  \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7906fa4c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b294f0e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da7a05b3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
